//该文件专门创建整个应用的路由器
import VueRouter from "vue-router";
//引入组件
import About from "@/pages/pages15/About";
import Home from "@/pages/pages15/Home";
import Message from "@/pages/pages15/Message";
import News from "@/pages/pages15/News";
import Detail from "@/pages/pages15/Detail";
//创建并暴露一个路由器
const router = new VueRouter({
    // mode:"history",
    routes:[
        {
            name:'about',
            path:'/about',
            component:About,
            meta:{isAuth:true,title:'关于'}
        },
        {
            name:'home',
            path:'/home',
            component:Home,
            meta:{title:'主页'},
            //二级路由
            children:[
                {
                    name:'message',
                 path:'message',
                 component:Message,
                    meta:{isAuth:true,title:'消息'},
                    //三级路由
                    children:[
                        {
                            name:'detail',
                            path:'detail/:id/:title',
                            component:Detail,
                            meta:{isAuth:true,title:'详情'},
                            //props的第一种写法，值为对象，该对象中的所有key-value都会以props形式传给detail组件
                            // props:{a:1,b:2}
                            //props的第二种写法，值为布尔值,若为真，就会把该路由组件收到的params参数，以props形式传给detail组件
                            // props:true
                            //props的第三种写法，值为函数，
                            props($route){
                                return {id:$route.query.id,title:$route.query.title,}
                            }
                        }
                    ]
                },
                {
                    name:'news',
                    path:'news',
                    component:News,
                    meta:{isAuth:true,title:'新闻'},
                    //独享路由守卫
                    // beforeEnter:()=>{
                    //     if (to.meta.isAuth){//判断是否鉴定权限
                    //         if (localStorage.getItem('school')==='atguigu'){
                    //             next()
                    //         }else {
                    //             alert('学校名不对，无权限！！！')
                    //         }
                    //     }else {
                    //         next()
                    //     }
                    // }
                },
            ]
        },

    ]
})
//全局前置路由守卫--每次路由切换之前被调用
router.beforeEach((to, from, next)=>{
console.log(to,from)
    // if (to.path==='/home/news'||to.path==='/home/message')
    if (to.meta.isAuth){//判断是否鉴定权限
        if (localStorage.getItem('school')==='atguigu'){
            next()
        }else {
            alert('学校名不对，无权限！！！')
        }
    }else {
        next()
    }

})
//全局后置路由守卫--每次路由切换之后被调用
router.afterEach((to, from)=>{
    console.log(to,from)
    document.title=to.meta.title || '学习系统'
})
export default router
